local extension = Package:new("wq_zhimi")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_zhimi"] = "无期迷途·执迷",
}

local Wqc = require "packages/FkWqmt/wq_common"

-- 禁闭者：哈梅尔
local wq_hameier = General:new(extension, "wq_hameier", "qun", 3, 3, General.Female)
-- 技能：安眠
local wq_skill_anmian = fk.CreateTriggerSkill{
  name = "wq_skill_anmian",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local roundNum = math.min(room:getTag("RoundCount"), 5)
    local tos = room:askForChoosePlayers(player, table.map(table.filter(room:getAlivePlayers(), function(p) return p:isWounded() and player:distanceTo(p) <= roundNum end), Util.IdMapper), 1, roundNum, "#wq_skill_anmian-choose:::"..roundNum, self.name, true)
    if #tos > 0 then
      room:sortPlayersByAction(tos)
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(self.cost_data) do
      local dest = room:getPlayerById(id)
      if dest:isWounded() then
        room:recover {
          who = dest,
          num = 1,
          recoverBy = player,
          skillName = self.name
        }
      end
    end
  end,
}
-- 技能：幻舞
local wq_skill_huanwu = fk.CreateTriggerSkill{
  name = "wq_skill_huanwu",
  anim_type = "support",
  events = {fk.PreHpRecover},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.recoverBy and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_huanwu_prompt::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    data.num = data.num + 1
  end,
}
-- 技能：命触
local wq_skill_mingchu = fk.CreateTriggerSkill{
  name = "wq_skill_mingchu",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and target:getMark("wq_skill_mingchu_recovered_"..player.id) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_mingchu_prompt::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage - 1
    if data.damage < 1 then
      return true
    end
  end,
  refresh_events = {fk.HpRecover},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target:getMark("wq_skill_mingchu_recovered_"..player.id) == 0 and data.who == target and data.recoverBy == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "wq_skill_mingchu_recovered_"..player.id, 1)
  end,
}
-- 哈梅尔的技能
wq_hameier:addSkill(wq_skill_anmian)
wq_hameier:addSkill(wq_skill_huanwu)
wq_hameier:addSkill(wq_skill_mingchu)
-- 哈梅尔的翻译
Fk:loadTranslationTable{
  ["wq_hameier"] = "哈梅尔",
  ["#wq_hameier"] = "群舞幻象",
  ["designer:wq_hameier"] = "妙霄",
  ["cv:wq_hameier"] = "官方",
  ["illustrator:wq_hameier"] = "官方",
  ["wq_skill_anmian"] = "安眠",
  [":wq_skill_anmian"] = "准备阶段，你可以令距离X以内至多X名已受伤的角色分别回复1点体力（X为当前游戏轮数且至多为5）。",
  ["$wq_skill_anmian"] = "安息吧，我带你们回家。",
  ["#wq_skill_anmian-choose"] = "安眠：你可以令距离%arg以内至多%arg名已受伤的角色分别回复1点体力",
  ["wq_skill_huanwu"] = "幻舞",
  [":wq_skill_huanwu"] = "每回合限一次，一名角色回复体力时，你可以令回复值+1。",
  ["$wq_skill_huanwu"] = "每一个不曾起舞的日子，都是对生命的辜负。",
  ["#wq_skill_huanwu_prompt"] = "幻舞：你可以令%dest本次回复值+1",
  ["wq_skill_mingchu"] = "命触",
  [":wq_skill_mingchu"] = "每回合限一次，一名角色受到伤害时，若你令其回复过体力，你可以令此伤害-1。",
  ["$wq_skill_mingchu"] = "破碎的灵魂唱着歌，它在为我们指引方向。",
  ["#wq_skill_mingchu_prompt"] = "命触：你可以令%dest本次受到的伤害-1",
  ["~wq_hameier"] = "曲终，舞停，人散……",
}


-- 禁闭者：堇
local wq_jin = General:new(extension, "wq_jin", "qun", 3, 3, General.Female)
-- 技能：无常
local wq_skill_wuchang = fk.CreateActiveSkill{
  name = "wq_skill_wuchang",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_wuchang_prompt"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Player.Hand and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and (not Fk:currentRoom():getPlayerById(to_select):isNude())
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cid = effect.cards[1]
    room:throwCard(cid, self.name, player, player)
    local num = math.min(2, #target:getCardIds("he"))
    local ids = room:askForCardsChosen(player, target, 1, num, "he", self.name)
    room:throwCard(ids, self.name, target, player)
    if player:isAlive() and target:isAlive() then
      table.insert(ids, cid)
      if #table.filter(ids, function(id) return Fk:getCardById(id).name == "jink" end) > 0 then
        room:damage{
          from = player,
          to = target,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end,
}
-- 技能：狂樱
local wq_skill_kuangying = fk.CreateTriggerSkill{
  name = "wq_skill_kuangying",
  anim_type = "offensive",
  events = {fk.EventPhaseStart, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Start
      elseif event == fk.Damage then
        return data.to.id ~= player.id and data.to:isAlive()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#wq_skill_kuangying_prompt-choose", self.name, true)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
    else
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_kuangying_prompt-damage::"..data.to.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dest = nil
    if event == fk.EventPhaseStart then
      dest = room:getPlayerById(self.cost_data)
    else
      dest = data.to
    end
    room:addPlayerMark(dest, "@wq_mark_liuxue", 1)
  end,
}
-- 技能：决意
local wq_skill_jueyi = fk.CreateTriggerSkill{
  name = "wq_skill_jueyi",
  anim_type = "special",
  events = {fk.FinishJudge, fk.PreHpLost},
  can_trigger = function(self, event, target, player, data)
    if event == fk.FinishJudge then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and data.skillName == "#wq_common_mark_liuxue_loseHp"
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.FinishJudge then
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_jueyi_prompt-FinishJudge")
    else
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_jueyi_prompt-PreHpLost::"..target.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.FinishJudge then
      room:drawCards(player, 1, self.name)
    else
      data.num = data.num + 1
    end
  end,
}
-- 堇的技能
wq_jin:addSkill(wq_skill_wuchang)
wq_jin:addSkill(wq_skill_kuangying)
wq_jin:addSkill(wq_skill_jueyi)
-- 堇的翻译
Fk:loadTranslationTable{
  ["wq_jin"] = "堇",
  ["#wq_jin"] = "武器操控",
  ["designer:wq_jin"] = "妙霄",
  ["cv:wq_jin"] = "官方",
  ["illustrator:wq_jin"] = "官方",
  ["wq_skill_wuchang"] = "无常",
  [":wq_skill_wuchang"] = "出牌阶段限一次，你可以弃置一张手牌，然后弃置一名其他角色的至多两张牌。若你或其以此法被弃置的牌中有【闪】，你对该角色造成1点伤害。",
  ["$wq_skill_wuchang"] = "别时方知此世中，花亦花来人亦人。",
  ["#wq_skill_wuchang_prompt"] = "无常：你可以弃置一张手牌，然后弃置一名其他角色的至多两张牌<br>若你或其以此法被弃置的牌中有【闪】，你对该角色造成1点伤害",
  ["wq_skill_kuangying"] = "狂樱",
  [":wq_skill_kuangying"] = "准备阶段/当你对其他角色造成伤害后，你可以令一名其他角色/该角色获得1枚「流血」标记。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_liuxue_description"),
  ["$wq_skill_kuangying"] = "落樱……落命！",
  ["#wq_skill_kuangying_prompt-choose"] = "狂樱：你可以令一名其他角色获得1枚「流血」标记",
  ["#wq_skill_kuangying_prompt-damage"] = "狂樱：你可以令%dest获得1枚「流血」标记",
  ["wq_skill_jueyi"] = "决意",
  [":wq_skill_jueyi"] = "一名角色的判定牌生效后，你可以摸一张牌。一名角色因“流血判定”而失去体力时，你可以令其失去体力的数值+1。",
  ["$wq_skill_jueyi"] = "此番路，来去无数，我亦在其中。",
  ["#wq_skill_jueyi_prompt-FinishJudge"] = "决意：你可以摸一张牌",
  ["#wq_skill_jueyi_prompt-PreHpLost"] = "决意：你可以令%dest此次失去体力的数值+1",
  ["~wq_jin"] = "抱歉，永别了……",
}

return extension